package cn.lijigou.shopping;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


import com.mapbar.android.maps.GeoPoint;
import com.mapbar.android.maps.ItemizedOverlay;
import com.mapbar.android.maps.MapActivity;
import com.mapbar.android.maps.MapController;
import com.mapbar.android.maps.MapView;
import com.mapbar.android.maps.OverlayItem;
import com.mapbar.android.maps.Projection;

public class MapItemActivity extends MapActivity {
	boolean flag = false;
	MapView mMapView;
	MapController mMapController;
	GeoPoint point;
	TipItemizedOverlay mTipItemizedOverlay;
	Drawable mTipBackground;
	Button button;
	/**
	 * 弹出的气泡View
	 */
	View popView;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_item_pop);

		mMapView = (MapView) findViewById(R.id.itemizedoverlayview);
		mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
		mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放

		point = new GeoPoint((int) (39.92183 * 1E6), (int) (116.34753 * 1E6)); // 用给定的经纬度构造一个GeoPoint，单位是微度
																				// (度
																				// *
																				// 1E6)
		mMapController.setCenter(point); // 设置地图中心点
		mMapController.setZoom(20);
		// Drawable marker = getResources().getDrawable(R.drawable.poi_1);
		// //得到需要标在地图上的资源
		// marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
		// .getIntrinsicHeight()); //为maker定义位置和边界

		// mMapView.getOverlays().add(new OverItemT(marker, this));
		// //添加ItemizedOverlay实例到mMapView

		mTipBackground = getResources().getDrawable(R.drawable.map_icon);
		mTipItemizedOverlay = new TipItemizedOverlay(this, mTipBackground);
		mMapView.getOverlays().add(mTipItemizedOverlay);
		button = (Button) findViewById(R.id.btn);
		button.setVisibility(View.VISIBLE);
		button.setOnClickListener(new ButtonListener());
	}

	class ButtonListener implements OnClickListener {

		
		public void onClick(View v) {
			Drawable marker = getResources().getDrawable(R.drawable.map_icon); // 得到需要标在地图上的资源
			marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); // 为maker定义位置和边界
			if (!flag) {
				button.setText("删除标点");
				mMapView.getOverlays().add(new OverItemT(marker, MapItemActivity.this));
				flag = true;
			} else {
				button.setText("标点");
				mMapView.getOverlays().clear();
				flag = false;
			}
		}
	}

	
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	class OverItemT extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();
		private Drawable marker;

		private double mLat1 = 39.92183; // point1纬度
		private double mLon1 = 116.34753; // point1经度

		private double mLat2 = 39.89494;
		private double mLon2 = 116.31526;

		private double mLat3 = 39.92183;
		private double mLon3 = 116.30906;

		public OverItemT(Drawable marker, Context context) {
			super(boundCenterBottom(marker));

			this.marker = marker;

			// 用给定的经纬度构造GeoPoint，单位是微度 (度 * 1E6)
			GeoPoint p1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
			GeoPoint p2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6));
			GeoPoint p3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6));

			// 构造OverlayItem的三个参数依次为：item的位置，标题文本，文字片段
			GeoList.add(new OverlayItem(p1, "北京图为先科技有限公司", "北京市西城区阜成门外大街2号万通大厦B座1606室"));
			GeoList.add(new OverlayItem(p2, "北京西站", "北京市丰台区莲花池东路"));
			GeoList.add(new OverlayItem(p3, "人民大学", "海淀区中关村大街59号"));
			populate(); // createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
		}

		
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {

			// Projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标系统之间的变换
			Projection projection = mapView.getProjection();
			for (int index = size() - 1; index >= 0; index--) { // 遍历GeoList
				OverlayItem overLayItem = getItem(index); // 得到给定索引的item

				String title = overLayItem.getTitle();
				// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
				Point point = projection.toPixels(overLayItem.getPoint(), null);
				Paint paintText = new Paint();
				paintText.setColor(Color.BLACK);
				paintText.setTextSize(15);
				// 绘制文本
				canvas.drawText(title, point.x, point.y - 25, paintText);
			}
			super.draw(canvas, mapView, shadow);
			// 调整一个durable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
			boundCenterBottom(marker);
		}

		
		protected OverlayItem createItem(int i) {
			// TODO Auto-generated method stub
			return GeoList.get(i);
		}

		
		public int size() {
			// TODO Auto-generated method stub
			return GeoList.size();
		}

		
		// 处理当点击事件
		protected boolean onTap(int i) {
			setFocus(GeoList.get(i));
			mTipItemizedOverlay.addOverlay(GeoList.get(i));
			return true;
		}
	}

	public void animateTo(GeoPoint pt) {
		mMapController.animateTo(pt);
	}
}
